package com.hspedu.List_;

import java.util.LinkedList;

public class LinkedList01 {
    public static void main(String[] args) {
        Node jack = new Node("jack");
        Node tom = new Node("tom");
        Node hsp = new Node("老韩");
        //连接三个节点
//        jack>tom>hsp
        jack.next = tom;
        tom.next = hsp;
        //hsp>tom>jack
        hsp.pre = tom;
        tom.pre = jack;

        Node first = jack;//让first引用指向jack,就是双向链表的头结点
        Node last = hsp;

        while(true){
            if(first==null)
            {break;}
            System.out.println(first);
            first=first.next;
        }
        last=hsp;
        System.out.println("==================");
        while(true){
            if(last==null)
            {break;}
            System.out.println(last);
            last=last.pre;
        }

        //演示链表的添加对象/数据
        //要求，是在 tom----------老韩  之间插入一个对象  smith


        Node smith = new Node("smith");
        smith.next=hsp;
        smith.pre=tom;
        tom.next=smith;
        hsp.pre=smith;
        System.out.println("==============");
        //重置first,让first指向第一个人
        first = jack;
        while(true){
            if(first==null)
            {break;}
            System.out.println(first);
            first=first.next;
        }
    }
}

//定义一个Node类，Node对象，表示双向链表的一个节点
class Node{
    public Object item;
    public Node next;
    public Node pre;

    public Node(Object item) {
        this.item = item;
    }

    @Override
    public String toString() {
        return "name: "  + item ;
    }
}
